<template>
  <el-button type="info" @click="displayAddDialog">新增</el-button>
  <div v-if="!addDialogDisplay">
  <el-table :data="tableData" style="width: 100%">
    <el-table-column fixed prop="name" label="工序名" width="150" />
    <el-table-column prop="equipment" label="设备名称/型号/工艺" width="120" />
    <el-table-column prop="price" label="工序单价" width="120" />
    <el-table-column fixed="right" label="Operations" min-width="120">
      <template #default="scope">
        <el-button link type="primary" size="small" @click="handleClick">
          修改
        </el-button>
        <el-button link type="primary" size="small" @click="deleteItem(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  </div>
  <div v-else>


    <app-base-step-table-add @submit="submitSuccess"></app-base-step-table-add>
  </div>
</template>

<script lang="ts" setup>
let { data }  = await useFetch('/api/step',{
  method:"get"
})

let tableData = reactive(data)

const displayAddDialog = ()=>{
  addDialogDisplay.value = true
}

const submitSuccess = async ()=>{
  addDialogDisplay.value = false
  const res  = await $fetch('/api/step',{
    method:"get"
  })
  tableData.value = res
  console.log(res)
}

const addDialogDisplay = ref(false)

const handleClick = () => {
  console.log('click')
}

const deleteItem =  async (row)=>{
  console.log(row._id)
  tableData.value = tableData.value?.filter(item=> item._id!=row._id)
  await $fetch(`/api/step/${row._id}`,{
    method :'delete',
  })
}
</script>
